/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ipercube.impl.simple;

import com.ipercube.Column;
import com.ipercube.DataMap;
import com.ipercube.Measure;
import com.ipercube.Pivot;
import com.ipercube.Row;
import com.ipercube.compiler.PivotCompiler;
import com.ipercube.model.ColumnModel;
import com.ipercube.DataSourceModel;
import com.ipercube.MeasureEvaluator;
import com.ipercube.model.RowModel;

/**
 *
 * @author tartur
 */
public class SimplePivotTable implements Pivot{
    private PivotCompiler compiler = new SimpleCompiler();
    private DataMap dataMap;
    private MeasureEvaluator evaluator;
    private DataSourceModel dataModel;
    private ColumnModel colModel;
    private RowModel rowModel;

    public Measure getMeasureAt(Row row, Column column) {
        return evaluator.evaluate(dataMap, row, column);
    }

    public void compile() {
        compiler.setColumnModel(colModel);
        compiler.setRowModel(rowModel);
        compiler.setDataModel(dataModel);
        dataMap = compiler.compile();
    }

    public DataSourceModel getDataModel() {
        return dataModel;
    }

    public void setDataModel(DataSourceModel dataModel) {
        this.dataModel = dataModel;
    }

    public ColumnModel getColumnModel() {
        return colModel;
    }

    public RowModel getRowModel() {
        return rowModel;
    }

    public void setColumnModel(ColumnModel colModel) {
        this.colModel = colModel;
    }

    public void setRowModel(RowModel rowModel) {
        this.rowModel = rowModel;
    }

    public void setMeasureEvaluator(MeasureEvaluator evaluator) {
        this.evaluator = evaluator;
    }

    public MeasureEvaluator getMeasureEvaluator() {
        return evaluator;
    }

    public PivotCompiler getCompiler() {
        return compiler;
    }

    public void setCompiler(PivotCompiler compiler) {
        this.compiler = compiler;
    }

    public DataMap getDataMap() {
        return dataMap;
    }

    public Row[] getRows() {
        int rowLevel = getRowModel().getMaxLevel();
        return getDataMap().getRowsByLevel(rowLevel).toArray(new Row[0]);
    }

    public Column[] getColumns() {
        int colLevel = getColumnModel().getMaxLevel();
        return getDataMap().getColumnsByLevel(colLevel).toArray(new Column[0]);
    }

}
